package cn.aylog.controller;

import org.apache.ibatis.javassist.bytecode.ByteArray;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStream;
import java.nio.CharBuffer;
import java.text.SimpleDateFormat;
import java.util.Arrays;

@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {

    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) {
        String filename = file.getOriginalFilename();
        return filename + "---size: " + file.getSize();
    }

    @PostMapping("/upload2")
    public String upload2(@RequestParam("file") String file) {
        String path = "d:/data/temp/a.txt";
        File temp = new File(path);
        try(OutputStream os = new FileOutputStream(temp)) {
            os.write(file.getBytes());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "SUCCESS";
    }

    @PostMapping("/upload3")
    public String upload3(@RequestParam("file") MultipartFile file) {
        String path = "d:/data/temp/temp_"+System.currentTimeMillis()+".tmp";
        System.out.println(path);
        File temp = new File(path);
        try(OutputStream os = new FileOutputStream(temp)) {
            os.write(file.getBytes());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "SUCCESS";
    }
}
